//
//  SYUserDataManger.m
//  siyanone
//
//  Created by LK-IOS on 2018/3/21.
//  Copyright © 2018年 LK-News. All rights reserved.
//

#import "SYUserDataManger.h"

@interface SYUserDataManger()


@end

@implementation SYUserDataManger


static SYUserDataManger *_instance;

+ (instancetype)allocWithZone:(struct _NSZone *)zone
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instance = [super allocWithZone:zone];
    });
    return _instance;
}

+ (instancetype)sharedInstance
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instance = [[self alloc] init];
    });
    return _instance;
}

- (id)copyWithZone:(NSZone *)zone
{
    return _instance;
}
-(void)deleteUserDataTable
{
    //清空个人信息
    LKKeyValueStore *store = [LKKeyValueStore sharedInstanceWithdbName:peizidbName];
    
    if ([store isExistTableWithName:peiziuserDataName]) {//存在个人信息表
        //删除原有用户信息
        [store clearTable:peiziuserDataName];
    }
}
-(BOOL)userTokenIsOver
{
    
    NSString *token = [self getUserToken];
    if (token) {
        return NO;
    }else{
        return YES;
    }
    
}

-(NSString *)getUserAccountNumberString
{
    NSString *accountString = [self getUserAccountNumber];
    return [NSString countNumAndChangeformat:[accountString pz_stringNumberByMultiplyingBy:@"1"]];
}

-(NSString *)getUserAccountNumber
{
    LKKeyValueStore *store = [LKKeyValueStore sharedInstanceWithdbName:peizidbName];
    NSMutableDictionary *getDict = (NSMutableDictionary *)[store getObjectById:peiziuserDataName fromTable:peiziuserDataName];
    //账户可用余额
    id obj = [getDict objectForKey:@"availableBalance"];
    NSString *accountString = [NSString stringWithFormat:@"%@",obj];
    
    return accountString;
}

-(NSString *)getUserToken
{
    LKKeyValueStore *store = [LKKeyValueStore sharedInstanceWithdbName:peizidbName];
    if ([store isExistTableWithName:peiziuserDataName]) {//存在个人信息表
        NSString *token = [store getStringById:@"userToken" fromTable:peiziuserDataName];
        if (token) {
            return token;
        }else{
            return nil;
        }
    }else{//不存在
        return nil;
    }
}

-(NSString *)getUerPhone
{
    LKKeyValueStore *store = [LKKeyValueStore sharedInstanceWithdbName:peizidbName];
    NSMutableDictionary *getDict = (NSMutableDictionary *)[store getObjectById:peiziuserDataName fromTable:peiziuserDataName];
    
    NSString *phone = [getDict objectForKey:@"phone"];
    
    return phone;
}


- (NSNumber *)getUser_Id
{
    LKKeyValueStore *store = [LKKeyValueStore sharedInstanceWithdbName:peizidbName];
    NSMutableDictionary *getDict = (NSMutableDictionary *)[store getObjectById:peiziuserDataName fromTable:peiziuserDataName];
    
    NSNumber *ID = [getDict objectForKey:@"id"];
    
    return ID;
}
-(BOOL)determineCurrentAmountSufficientWithNeedAmountString:(NSString *)amountString
{
    NSString *useAmount = [self getUserAccountNumber];
    //获取差值
    NSString *difference = [useAmount pz_stringNumberBySubtracting:amountString];
    if ([difference floatValue] >= 0) {
        return YES;
    }else{
        return NO;
    }
}
-(void)currentAccountBalanceChangeWithUsedAmountString:(NSString *)amountString IsAdd:(BOOL)isAdd
{
    NSString *useAmount = [self getUserAccountNumber];
    
    
    NSString *endAmountString;
    if (isAdd) {
        endAmountString = [useAmount pz_stringNumberByAdding:amountString];
    }else{
        endAmountString = [useAmount pz_stringNumberBySubtracting:amountString];
    }
    
    LKKeyValueStore *store = [LKKeyValueStore sharedInstanceWithdbName:peizidbName];
    //获取个人信息表
    NSDictionary *getDict = (NSDictionary *)[store getObjectById:peiziuserDataName fromTable:peiziuserDataName];
    NSMutableDictionary *dict = [getDict mutableCopy];
    [dict removeObjectForKey:@"availableBalance"];
    [dict setObject:endAmountString forKey:@"availableBalance"];
    
    [store putObject:dict withId:peiziuserDataName intoTable:peiziuserDataName];
    
}

-(void)storeUserDataWithDict:(NSDictionary *)dict
{
    //获取数据库
    LKKeyValueStore *store = [LKKeyValueStore sharedInstanceWithdbName:peizidbName];
    
    if ([store isExistTableWithName:peiziuserDataName]) {//存在个人信息表
        //删除原有用户信息
        [store deleteObjectById:peiziuserDataName fromTable:peiziuserDataName];
        
        NSMutableDictionary *getDict = [NSMutableDictionary dictionary];
        
        //phone
        [getDict setObject:dict[@"phone"] forKey:@"phone"];
        //userID
        [getDict setObject:dict[@"serialCode"] forKey:@"userID"];
        //账户余额
        [getDict setObject:dict[@"balance"] forKey:@"balance"];
        if ([dict objectForKey:@"headPortrait"]) {
            //用户头像
            [getDict setObject:dict[@"headPortrait"] forKey:@"headPortrait"];
        }
        else
        {
            //用户头像
            [getDict setObject:@"" forKey:@"headPortrait"];
        }
        
        
        //账户可用余额
        [getDict setObject:dict[@"availableBalance"] forKey:@"availableBalance"];
        //用户的id
        [getDict setObject:dict[@"id"] forKey:@"id"];
        //存储
        [store putObject:getDict withId:peiziuserDataName intoTable:peiziuserDataName];
        
        
    }else{//不存在
        [store createTableWithName:peiziuserDataName];
        NSMutableDictionary *creatDict = [NSMutableDictionary dictionary];
        //将个人需要保存的信息放进dict，将dict存进个人信息表
        
        //phone
        [creatDict setObject:dict[@"phone"] forKey:@"phone"];
        //userID
        [creatDict setObject:dict[@"serialCode"] forKey:@"userID"];
        //账户余额
        [creatDict setObject:dict[@"balance"] forKey:@"balance"];
        
        //用户头像
        //[creatDict setObject:dict[@"headPortrait"] forKey:@"headPortrait"];
        
        if ([dict objectForKey:@"headPortrait"]) {
            //用户头像
            [creatDict setObject:dict[@"headPortrait"] forKey:@"headPortrait"];
        }
        else
        {
            //用户头像
            [creatDict setObject:@"" forKey:@"headPortrait"];
        }
        //账户可用余额
        [creatDict setObject:dict[@"availableBalance"] forKey:@"availableBalance"];
        //用户的id
        [creatDict setObject:dict[@"id"] forKey:@"id"];
        
        
        [store putObject:creatDict withId:peiziuserDataName intoTable:peiziuserDataName];
    }
}
-(NSDictionary *)getUserDict
{
    LKKeyValueStore *store = [LKKeyValueStore sharedInstanceWithdbName:peizidbName];
    NSMutableDictionary *getDict = (NSMutableDictionary *)[store getObjectById:peiziuserDataName fromTable:peiziuserDataName];
    return getDict;
}
-(void)storeUserToken:(NSString *)tokenString
{
    LKKeyValueStore *store = [LKKeyValueStore sharedInstanceWithdbName:peizidbName];
    if ([store isExistTableWithName:peiziuserDataName]) {//存在个人信息表
        [store putString:tokenString withId:@"userToken" intoTable:peiziuserDataName];
    }else{//不存在
        [store createTableWithName:peiziuserDataName];
        [store putString:tokenString withId:@"userToken" intoTable:peiziuserDataName];
    }
}


-(void)storeUserTips:(BOOL)isTips
{
    LKKeyValueStore *store = [LKKeyValueStore sharedInstanceWithdbName:peizidbName];
    if ([store isExistTableWithName:peiziuserDataName]) {//存在个人信息表
        if (isTips) {
            [store putString:@"YES" withId:@"isTips" intoTable:peiziuserDataName];
        }else{
            [store putString:@"NO" withId:@"isTips" intoTable:peiziuserDataName];
        }
        
    }else{//不存在
        [store createTableWithName:peiziuserDataName];
        if (isTips) {
            [store putString:@"YES" withId:@"isTips" intoTable:peiziuserDataName];
        }else{
            [store putString:@"NO" withId:@"isTips" intoTable:peiziuserDataName];
        }
    }
}

-(BOOL)getUserTips
{
    LKKeyValueStore *store = [LKKeyValueStore sharedInstanceWithdbName:peizidbName];
    if ([store isExistTableWithName:peiziuserDataName]) {//存在个人信息表
        NSString *userTips = [store getStringById:@"isTips" fromTable:peiziuserDataName];
        if ([userTips isEqualToString:@"YES"] && userTips.length) {
            return YES;
        }else{
            return NO;
        }
    }else{//不存在
        return NO;
    }
}


@end
